<?php

# accessing substring
$email = "williamcnkr.live.com";


if (strpos($email, '@') === false) {
    print "There was no @ in the a mail address";
}

echo "</br>";

# Extracting Substrings
#$substring = substr($string,$start,$length);
#$username = substr($_GET['username'],0,8);

#If $start and $length are positive, substr() returns $length characters in the string, starting at $start. 
echo substr('watch out for that tree',6,5);
echo "</br>";

# If you leave out $length, substr() returns the string from $start to the end of the original string,
print substr('watch out for that tree',17);
echo "</br>";

# If $start plus $length goes past the end of the string, substr() returns all of the string from $start forward
print substr('watch out for that tree',20,5);
echo "</br>";

# With a negative $start value that goes past the beginning of the string
print substr('watch out for that tree',15,-2); 
echo "</br>";
print substr('watch out for that tree',-4,-1);
echo "</br>";

#  Replacing Substrings
// Everything from position $start to the end of $old_string
// becomes $new_substring
print substr_replace('My pet is a blue dog.','fish.',12);
echo "</br>";
print substr_replace('My pet is a blue dog.','green',12,4); 
echo "</br>";
$credit_card = '4111 1111 1111 1111';
echo "</br>";
print substr_replace($credit_card,'xxxx ',0,strlen($credit_card)-4);
echo "</br>";

# LookandSay Processing a String One Byte at a Time


echo "</br>";
#Reversing a String by Word or Byte
$s = " Once upon a time there was a turtle.";
$reversed_s = implode(' ',array_reverse(explode(' ',$s)));
echo $reversed_s;
echo "</br>";

# Grenerating a random string

#echo str_rand(15,"我是中国人民解放68233部队72分队3排八班战士");
echo "</br>";

# Controling Case
print ucfirst("how do you do today?"); 
echo "</br>";
print ucwords("the prince of wales");
echo "</br>";
print strtoupper("i'm not yelling!");
echo "</br>";
print strtolower('<A HREF="one.php">one</A>');
echo "</br>";

# Interpolating Functions and Expressions Within Strings

/*
print 'You have '.($_POST['boys'] + $_POST['girls']).' children.';
print "The word '$word' is ".strlen($word).' characters long.';
print 'You owe '.$amounts['payment'].' immediately.';
print "My circle's diameter is ".$circle->getDiameter().' inches.';


print "I have $children children.";
print "You owe $amounts[payment] immediately.";
print "My circle's diameter is $circle->diameter inches.";

print "I have {$children} children.";
print "You owe {$amounts['payment']} immediately.";
print "My circle's diameter is {$circle->getDiameter()} inches.";

*/

# Trimming Blanks from a String
echo "</br>";
#$zipcode = trim($_GET['zipcode']);
#$no_linefeed = rtrim($_GET['text']);
#$name = ltrim($_GET['name']);

#1.11 Generating Comma-Separated Data


$books = array( array('Elmer Gantry', 'Sinclair Lewis', 1927), array('The Scarlatti Inheritance','Robert Ludlum', 1971),
    array('The Parsifal Mosaic','William Styron', 1979) );

foreach ($books as $book) {
    print pack('A25A15A4', $book[0], $book[1], $book[2]) . "\n";
}

#wordwrap

$s = "Four score and seven years ago our fathers brought forth on this continent ↵ a new nation, conceived in liberty and dedicated to the proposition ↵
    that all men are created equal.";
print "<pre>\n".wordwrap($s)."\n</pre>";
echo "</br>";
print wordwrap($s,50);


